import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    timer: null,
    cityName: "淮安",
    form: {
      contactName: "",
      contactPhone: "",
      enterpriseCode: "",
      enterpriseName: "",
      loanAmount: "",
      isoneloan: false,
      ispledge: false,
      pledgegood: ""
    },
    lianxiren: "",
    banklists: [],
    banklist: [],
    selectbanklist: [],
    proselectisshow: false,
    applysuccessdialog: false,
    searchbank: "",
    // 企业列表下拉框
    dropdownIsShow: false,
    prolist: [],
    prosearchlodding: false,
    proinputisfocus: false,
  },



  proitemClick(e) {
    console.log(e.currentTarget.dataset.proname);
    this.setData({
      ['form.enterpriseName']: e.currentTarget.dataset.proname,
      ['form.enterpriseCode']: e.currentTarget.dataset.prokey,
    })
    console.log(this.data, 'data1111111');
    this.setData({
      dropdownIsShow: false,
      prolist: [],
      proinputisfocus: true
    })
  },
  procodeFocus() {
    this.closedropdown()
  },
  procodeBlur() {
    var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    if (reg.test(this.data.form.enterpriseCode)) {
      Toast("企业证件代码格式不正确！");
    } else {
      // Toast("没有包含汉字！");
    }
  },
  // 代码
  setprocode(e) {
    console.log(e);
    this.setData({
      ['form.enterpriseCode']: e.detail.value
    })
  },
  setlianxiren(e) {
    this.setData({
      ['form.contactName']: e.detail.value
    })
  },
  setlianxirenphone(e) {
    var reg = /^1[3-9]\d{9}$/
    let value = e.detail.value.replace(/\D/g, '')
    // console.log(value);
    // if (reg.test(value)) {
    //   // Toast('贷款金额上限不可超过1000000万元！');
    // } else {
    //   Toast("手机号格式不正确")
    // }
    this.setData({
      ['form.contactPhone']: value
    })
  },
  // 意向贷款银行取消选中项
  closebankClick(e) {
    //console.log(e.currentTarget.dataset.tagindex);
    const name = e.currentTarget.dataset.name;
    console.log(this.data, 'data');
    // console.log(this.data.banklist, 'this.data.banklist');
    const banklist = getApp().globalData.banklist
    const findIndex = banklist.findIndex(val => val === name)
    banklist.splice(findIndex, 1)
    // console.log(this.data.banlist, 'banlist');
    this.setData({
      banklist: banklist
    })
    getApp().globalData.banklist = banklist;
  },
  seletbanklistIshow() {
    wx.navigateTo({
      url: '/pages/bankList/bankList',
    })
  },
  setloanmoney(e) {
    console.log(e, '文本框内容')
    // if (this.data.form.loanAmount > 10000) {
    if (Number(e.detail.value) > 10000) {
      Toast('贷款金额上限不可超过10000万元！');
      this.setData({
        ['form.loanAmount']: 10000
      })
    } else {
      this.setData({
        ['form.loanAmount']: this.validateNumber(e.detail.value)
      })
    }
  },
  switchChange(e) {
    this.setData({
      ['form.isoneloan']: e.detail
    })
  },
  ispledgeChange(e) {
    this.setData({
      ['form.ispledge']: e.detail
    })
    if (e.detail == false) {
      this.setData({
        ['form.pledgegood']: ''
      })
    }
  },
  // 前往个人中心
  toindexClcik() {
    wx.switchTab({
      url: '/pages/Home/Home',
    })
  },
  // 抵押物
  setpledgegood(e) {
    this.setData({
      ['form.pledgegood']: e.detail.value
    })
  },
  // 企业名称变化
  setproname(e) {
    this.setData({
      ['form.enterpriseName']: e.detail.value,
    })
    // 代码实现
    if (this.data.form.enterpriseName.length > 4) {
      this.setData({
        ['timer']: new Date().getTime()
      })
      this.getProlist(this.data.timer)
    } else {
      this.setData({
        ['dropdownIsShow']: false
      })
    }
  },
  getProlist(timer) {
    console.log(timer, 'timertimer')
    const _this = this
    let prams = {
      // corpName: this.data.form.enterpriseName,
      // pageNum: 1,
      // pageSize: 10,
      // region: this.data.commonObj.cityName
      corpName: this.data.form.enterpriseName
    }
    console.log(prams, 'pramsprams');
    this.setData({
      prosearchlodding: true
    })
    if (this.data.form.enterpriseName.length > 0) {
      getApp().globalData.request('/hxd/getCorpsByName', prams, "POST", ).then(res => {
        console.log("++++++++", res);
        if (timer === this.data.timer) {
          if (res.data.records.length > 0) {
            _this.setData({
              dropdownIsShow: true,
              prolist: res.data.records,
              prosearchlodding: false
            })
          } else {
            _this.setData({
              prolist: [],
              prosearchlodding: false
            })
            this.closedropdown()
          }
        }
      })
    } else {
      _this.setData({
        prolist: []
      })
      this.closedropdown()
    }
  },
  closedropdown() {
    this.setData({
      dropdownIsShow: false
    })
  },
  // 提交申请
  submitApply() {
    var reg = /^1[3-9]\d{9}$/
    var arr = []
    console.log(this.data.banklists, 'this.data.banklists');
    if (this.data.banklists.length > 0) {
      this.data.banklist.forEach((item, index) => {
        this.data.banklists.forEach((jitm, jindex) => {
          if (item == jitm.bankName) {
            let obj = {
              id: jitm.id,
              bankName: jitm.bankName
            }
            arr.push(obj)
          }
        })
      })
    }
    // console.log(this.data.form, 'this.data.form');
    if (this.data.form.contactName !== '' && this.data.form.contactPhone !== '' && this.data.form.loanAmount !== '' && this.data.form.enterpriseName !== '' && this.data.form.enterpriseCode !== '') {
      if (reg.test(this.data.form.contactPhone)) {
        if (this.data.form.ispledge && this.data.form.pledgegood == "") {
          Toast("请先输入您要抵押的物品！")
        } else {
          let pardata = {
            contactName: this.data.form.contactName,
            contactPhone: this.data.form.contactPhone,
            enterpriseName: this.data.form.enterpriseName,
            enterpriseCode: this.data.form.enterpriseCode,
            bankList: arr,
            loanAmount: this.data.form.loanAmount,
            isFirst: this.data.form.isoneloan ? 1 : 0,
            city: this.data.cityName,
            // 添加抵押物字段
            isMortgage: this.data.form.ispledge ? 1 : 0,
            pledge: this.data.form.pledgegood
          }

          console.log(pardata);
          getApp().globalData.request("/financing/submitApply", pardata, "POST").then(res => {
            console.log(res);
            if (res.code == 1) {
              // this.setData({
              //   applysuccessdialog: true
              // })
              Toast("您的需求已提交成功，可在申请记录中查看进度。")
              // 清除表单
              // this.clearForm()
              wx.redirectTo({
                url: '/pages/main/main?type=4',
              })
            } else {
              Toast(res.message);
            }
          })
        }
      } else {
        Toast("您输入的手机号不正确,请重新输入！")
      }
    } else {
      Toast('请先完善信息！');
    }
  },
  loanmoneyblur() {
    if (this.data.form.loanAmount > 100000) {
      Toast('贷款金额上限不可超过10亿元！');
      this.setData({
        ['form.loanAmount']: 100000
      })
    } else {
      this.setData({
        ['form.loanAmount']: this.validateNumber(this.data.form.loanAmount)
      })
    }
  },
  validateNumber(val) {
    console.log(val, 'valval')
    return val.toString().replace(/\D/g, '')
  },
  async getBankList() {
    const {
      data
    } = await getApp().globalData.request("/financing/getOrderedBanks", {city: '淮安市'}, "POST")
    console.log(data, '意向贷款银行');
    if (data.bankList.length > 0) {
      this.setData({
        banklists: data.bankList
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const userInfo = wx.getStorageSync('userInfo') || ''
    if (userInfo && userInfo.phone) {
      this.setData({
        ['form.contactPhone']: userInfo.phone
      })
      this.getBankList()
    } else {
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/main/main?type=4',
        })
      }, 2000);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      banklist: getApp().globalData.banklist,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})